﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using UserClient.ServiceReference1;
using UserClient;

namespace UserClient
{
    public partial class OrderWindow : Form
    {
        private StandardServiceClient stdService = new StandardServiceClient();
        private List<WCFPlane> planeList = new List<WCFPlane>();
        private bool fclass = false;
        private int check = 0;
        private WCFCompleteRoute completeRoute;
        private double totalPrice = 0;
        private Thread locktime;

        public OrderWindow()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            userInfoPanel.Hide();
            travelSelectionPanel.Show();
            LoadData();
        }

        private void LoadData()
        {
            List<WCFDestination> destlist = stdService.FindAllDestinations();

            foreach (WCFDestination item in destlist)
            {
                fromComboBox.Items.Add(item.Name);
            }
            foreach (WCFDestination item in destlist)
            {
                toComboBox.Items.Add(item.Name);
            }
            for (int i = 1; i <= 4; i++)
            {
                seatComboBox.Items.Add(i);
            }

        }

        private void selectionNextButton_Click(object sender, EventArgs e)
        {
            //dbPlane.FillHerUp();

            if (cheapRadioButton.Checked)
            {
                check = 1;
            }
            else if (fastRadioButton.Checked)
            {
                check = 2;
            }

            if (fromComboBox.SelectedItem == toComboBox.SelectedItem)
            {
                label3.Text = "From and To destinations cannot be the same!";
            }
            else
            {

                if (firstClassCheckBox.Checked)
                {
                    if (stdService.CalculateShortestPath(fromComboBox.Text, toComboBox.Text, check, dateTimePicker.Value, seatComboBox.SelectedIndex + 1, true).BestTotalCost != 0)
                    {
                        completeRoute = stdService.CalculateShortestPath(fromComboBox.Text, toComboBox.Text, check, dateTimePicker.Value, seatComboBox.SelectedIndex + 1, true);
                        RouteWindow rw = new RouteWindow(this.selectRoute, fromComboBox.Text, toComboBox.Text, createPriceAndTime(fromComboBox.Text, toComboBox.Text, completeRoute, 1), FindFlyingPlanes(fromComboBox.Text, dateTimePicker.Value, completeRoute), completeRoute);
                        rw.ShowDialog();
                    }
                    else
                    {
                        label3.Text = "Not possible!";
                    }


                }
                else
                {
                    if (stdService.CalculateShortestPath(fromComboBox.Text, toComboBox.Text, check, dateTimePicker.Value, seatComboBox.SelectedIndex + 1, false).BestTotalCost != 0)
                    {
                        completeRoute = stdService.CalculateShortestPath(fromComboBox.Text, toComboBox.Text, check, dateTimePicker.Value, seatComboBox.SelectedIndex + 1, false);
                        RouteWindow rw = new RouteWindow(this.selectRoute, fromComboBox.Text, toComboBox.Text, createPriceAndTime(fromComboBox.Text, toComboBox.Text, completeRoute, 0), FindFlyingPlanes(fromComboBox.Text, dateTimePicker.Value, completeRoute), completeRoute);
                        rw.ShowDialog();
                    }
                    else
                    {
                        label3.Text = "Not possible!";
                    }
                }

            }
        }

        private void selectRoute()
        {
            this.Text = "User Information";
            travelSelectionPanel.Hide();
            stdService.ReserveSeats(1, planeList, seatComboBox.SelectedIndex + 1);
            userInfoPanel.Show();
            locktime = new Thread(lockout);
            locktime.Start();
        }

        public void lockout()
        {
            DateTime unlocktime = DateTime.Now.AddMinutes(5);
            while (DateTime.Now < unlocktime)
            {
               
            }
            stdService.UnreserveSeats(1, planeList, seatComboBox.SelectedIndex + 1);
        }

        private void userAcceptButton_Click(object sender, EventArgs e)
        {
            string fullName = firstNameTextBox.Text + " " + lastNameTextBox.Text;

            if (firstClassCheckBox.Checked)
            {
                fclass = true;
            }
            else
            {
                fclass = false;
            }

            if(stdService.createUserAndOrder(fullName, phoneNumberTextBox.Text, mailTextBox.Text, totalPrice, completeRoute.BestAllDestinations))
            {
                DialogResult okBox = MessageBox.Show("Order has been created!", "OK", MessageBoxButtons.OK);
                if (okBox == System.Windows.Forms.DialogResult.OK)
                {
                    locktime.Abort();
                    this.Close();
                }             
            }
            else
            {
                DialogResult okbox = MessageBox.Show("An error has occured, please try again!", "OK", MessageBoxButtons.OK);
            }
        }

        private void userBackButton_Click(object sender, EventArgs e)
        {
            this.Text = "Travel Selection";
            userInfoPanel.Hide();
            travelSelectionPanel.Show();
        }

        private string[] createPriceAndTime(string fromDestination, string toDestination, WCFCompleteRoute completeRoute, int firstClassCheck)
        {
            totalPrice = 0;
            double travelTime = 0;
            int x;
            if (completeRoute.BestAllDestinations != null)
            {
                string[] destinations = completeRoute.BestAllDestinations.Split(' ');

                totalPrice += stdService.FindRoute(fromDestination, destinations[0]).Price;
                travelTime += stdService.FindRoute(fromDestination, destinations[0]).Time;

                for (x = 1; x < destinations.Length; x++)
                {
                    totalPrice += stdService.FindRoute(destinations[x - 1], destinations[x]).Price;
                    travelTime += stdService.FindRoute(destinations[x - 1], destinations[x]).Time;
                }
            }
            else
            {
                totalPrice = stdService.FindRoute(fromDestination, toDestination).Price;
                travelTime = stdService.FindRoute(fromDestination, toDestination).Time;
            }
            
            if (firstClassCheck == 1)
            {
                totalPrice = totalPrice * 2;
            }
            string price = Convert.ToString(totalPrice);
            string time = Convert.ToString(travelTime);
            

            string[] priceAndTime = { price, time };

            return priceAndTime;
        }


        private List<WCFPlane> FindFlyingPlanes(string fromDestination, DateTime dateTimePicker, WCFCompleteRoute completeRoute)
        {
            WCFPlane plane = new WCFPlane();
            string[] destinationer = completeRoute.BestAllDestinations.Split(' ');
            int x;

            plane = stdService.FindPlane(dateTimePicker.Date, stdService.FindRoute(fromDestination, destinationer[0]).RouteId);
            planeList.Add(stdService.FindPlane(dateTimePicker.Date, stdService.FindRoute(fromDestination, destinationer[0]).RouteId));

            for (x = 1; x < destinationer.Length; x++)
            {
                if (plane.Arrival >= dateTimePicker.Date)
                {
                    planeList.Add(stdService.FindPlane(dateTimePicker.Date, stdService.FindRoute(destinationer[x - 1], destinationer[x]).RouteId));
                }
            }
            return planeList;
        }

        private void travelSelectionPanel_Paint(object sender, PaintEventArgs e)
        {

        }
    }


}
